package com.zkingsoft.actions.admin.distribution;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import com.zkingsoft.services.distribution.DistributionCategoryService;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.distribution.DistributionCategory;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.util.WebUtil;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.pojo.AjaxResult;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zkingsoft.anotations.RemoveRequestToken;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-06-23 09:14
 */
@Controller
@RequestMapping(value = "admin/distributionCategory")
public class DistributionCategoryController extends BaseController{

	@Resource
	private DistributionCategoryService distributionCategoryService;
	
	@Resource
	private MatrixAdapter matrixAdapter;
	
	
	
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="DistributionCategory_BEV";
	
	public static final String fnCode = "DistributionCategory";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(DistributionCategory distributionCategory, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(distributionCategoryService, distributionCategory, pageVo);
	}
   	
	/**
	 * 新增或者修改页面
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(DistributionCategory distributionCategory) {
		if (distributionCategory.getCatId() != null) {
	   		matrixAdapter.isBtnPermitted(edit);
	   		distributionCategory.setCatId(((DistributionCategory)WebUtil.getSessionAttribute(BEV)).getCatId());
	   		AjaxResult result=modify(distributionCategoryService, WebUtil.getSessionAttribute(BEV), distributionCategory, "产品分类");
			WebUtil.removeSessionAttribute(BEV);
			return  result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			return add(distributionCategoryService, distributionCategory, "产品分类");
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		DistributionCategory distributionCategory;
		if (id != null) {
			distributionCategory = distributionCategoryService.findById(id);
			WebUtil.getRequest().setAttribute("obj", distributionCategory);
			WebUtil.setSessionAttribute(BEV, distributionCategory);
		}
		return "admin/distribution/DistributionCategory-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(Long catId) {
		matrixAdapter.isBtnPermitted(del);
		int i=distributionCategoryService.removeById(catId);
		if(i>0){
			return new AjaxResult(AjaxResult.STATUS_OK,"删除成功");
		}
		return new AjaxResult(AjaxResult.STATUS_ERR,"删除失败");
	}
	
	/**
	 * 查询所有分类
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all(DistributionCategory distributionCategory) {
		matrixAdapter.isBtnPermitted(search);
		List<DistributionCategory> dataList = distributionCategoryService.findByModel(distributionCategory);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, dataList);
		return result;
	}
	/**
	 * 查询所有分类
	 */
	@RequestMapping(value = "/allBase")
	public @ResponseBody AjaxResult allBase(DistributionCategory distributionCategory, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		distributionCategory.setCatParentId((long) 0);
		List<DistributionCategory> dataList = distributionCategoryService.findByModel(distributionCategory);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, dataList);
		return result;
	}
	
	/**
	 * 根据ID查询
	 */
	@RequestMapping(value = "/findById")
	public @ResponseBody AjaxResult findById(DistributionCategory distributionCategory) {
		distributionCategory=distributionCategoryService.findById(distributionCategory.getCatId());
		WebUtil.setSessionAttribute(BEV, distributionCategory);
		return new AjaxResult(AjaxResult.STATUS_OK, Arrays.asList(distributionCategory));
	}
	
	/**
	 * 新增或者修改页面
	 */   
   	@RequestMapping(value = "/modify")
	public @ResponseBody AjaxResult modify(DistributionCategory distributionCategory) {
		if (distributionCategory.getCatId() != null) {
	   		matrixAdapter.isBtnPermitted(edit);
//	   		distributionCategory.setCatId(((DistributionCategory)WebUtil.getSessionAttribute(BEV)).getCatId());
	   		AjaxResult result = modify(distributionCategoryService, WebUtil.getSessionAttribute(BEV), distributionCategory, "产品分类");
			WebUtil.removeSessionAttribute(BEV);
			return  result;
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, "修改失败！");
	}
}